/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "set_key_event_serializer.h"
#include <map>
#include "cJSON.h"
#include "cjson_check.h"
#include "key_event.h"

namespace OHOS {
namespace EDM {

const char* const KEY_EVENT = "keyEvent";
const char* const KEY_POLICY = "keyPolicy";

bool SetKeyEventsSerializer::Deserialize(const std::string &policy,
    std::vector<KeyCustomization> &dataObj)
{
    if (policy.empty()) {
        return true;
    }
    cJSON* root = cJSON_Parse(policy.c_str());
    if (root == nullptr) {
        return false;
    }
    cJSON* mapItem;
    cJSON_ArrayForEach(mapItem, root) {
        cJSON* keyEvent = cJSON_GetObjectItem(mapItem, KEY_EVENT);
        cJSON* keyPolicy = cJSON_GetObjectItem(mapItem, KEY_POLICY);

        KeyCustomization keyEventsInfo;
        keyEventsInfo.keyEvent = cJSON_GetNumberValue(keyEvent);
        keyEventsInfo.keyPolicy = cJSON_GetNumberValue(keyPolicy);
        dataObj.emplace_back(keyEventsInfo);
    }
    cJSON_Delete(root);
    return true;
}

bool SetKeyEventsSerializer::Serialize(const std::vector<KeyCustomization> &dataObj,
    std::string &policy)
{
    if (dataObj.empty()) {
        return true;
    }
    cJSON* root = nullptr;
    CJSON_CREATE_OBJECT_AND_CHECK(root, false);
    for (auto& mapIt : dataObj) {
        cJSON* policyObject = nullptr;
        CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(policyObject, false, root);
        cJSON_AddNumberToObject(policyObject, KEY_EVENT, mapIt.keyEvent);
        cJSON_AddNumberToObject(policyObject, KEY_POLICY, mapIt.keyPolicy);
        if (!cJSON_AddItemToArray(root, policyObject)) {
            cJSON_Delete(root);
            cJSON_Delete(policyObject);
            return false;
        }
    }
    char *cJsonStr = cJSON_Print(root);
    if (cJsonStr != nullptr) {
        policy = std::string(cJsonStr);
        cJSON_free(cJsonStr);
    }
    cJSON_Delete(root);
    return true;
}

bool SetKeyEventsSerializer::GetPolicy(MessageParcel &data,
    std::vector<KeyCustomization> &result)
{
    int32_t size = data.ReadInt32();
    for (int32_t i = 0; i < size; i++) {
        KeyCustomization keyEvent;
        if (!KeyEventHandle::ReadKeyCustomization(data, keyEvent)) {
            EDMLOGE("SetKeyEventsSerializer GetDataByParcel::read parcel fail");
        }
        result.emplace_back(keyEvent);
    }
    return true;
}

bool SetKeyEventsSerializer::WritePolicy(MessageParcel &reply,
    std::vector<KeyCustomization> &result)
{
    return true;
}

bool SetKeyEventsSerializer::MergePolicy(std::vector<std::vector<KeyCustomization>> &data,
    std::vector<KeyCustomization> &result)
{
    return true;
}

} // namespace EDM
} // namespace OHOS